<?php

$this->freshSession();
if(isset($_GET['out_trade_no'])){
	    var_dump($_GET);exit;
		$con = mysql_connect("127.0.0.1","root",'root');
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		
		mysql_select_db("shuju", $con);
		mysql_query("set names 'utf8'");
		$orderid = $_GET['out_trade_no'];
	
		$addtime = time();
	 	$result2 = mysql_query("select * from ssc_order where order_number='{$orderid}' and state = 0");	
	 	//var_dump($result2);
		if(mysql_num_rows($result2) == 1)
		{      
			$row = mysql_fetch_array($result2);
			$ovalue = $row['recharge_amount'];
			mysql_query("update ssc_order set state=1 where order_number='{$orderid}' and state = 0");
			mysql_query("update ssc_member_recharge set state='9',rechargeAmount={$ovalue},actionTime={$addtime},rechargeTime={$addtime} where  rechargeId='".$orderid."'"); 
	        mysql_query("update ssc_members set coin=coin+{$ovalue} where  uid={$row['username']}");	        
			$r  = mysql_query("select coin from ssc_members where  uid={$row['username']}");
			$rr = mysql_fetch_array($r);			
			mysql_query("insert into ssc_coin_log (`id`,`uid`,`type`,`playedId`,`coin`,`userCoin`,`fcoin`,`liqType`,`actionUID`,`actionTime`,`actionIP`,`info`,`extfield0`,`extfield1`,`extfield2`) values(null,'{$row['username']}','0','0','{$ovalue}','{$rr['coin']}','0','1','0','".time()."','0','充值','258','','0')");
        }

        header("Location:http://www.boyi333.com/#/ucenter/index");
	

}

if($this->user['uid']){
	$uid=$this->user['uid'];
	$amount=$_REQUEST['amount'];
	$rechargeId=$this->getRechId();
	$time=date('Y-m-d H:i:s', time());
	//echo json_encode(array('amount'=>$amount));exit;
	//echo $amount;exit;
	if(empty($amount)) {
		echo '<script>alert("卡密不能为空");</script>';exit;
	}
	if(empty($uid)) {
		echo '<script>alert("用户登录失效");</script>';exit;
	}
	$kmsql="select * from km where km=? and status=0";
	$kminfo=$this->getRow($kmsql, $amount);
	//print_r($kminfo);exit;
	if(!$kminfo) {
		echo '<script>alert("此卡密已被使用！");</script>';exit;
		$data=array('code'=>0,'result'=>'此卡密已被使用！');
		echo json_encode($data);exit;
	}
	if($this->update("INSERT INTO {$this->prename}order (order_number, username, recharge_amount, state, time) VALUES('{$rechargeId}', '{$uid}', '{$kminfo['fee']}', '0', '{$time}')")) {
		$para=array();
		$para['mBankId']='123';
		$para['amount']=floatval($kminfo['fee']);
		$para['rechargeId']=$rechargeId;
		$para['actionTime']=$this->time;
		$para['uid']=$this->user['uid'];
		$para['username']=$this->user['username'];
		$para['actionIP']=$this->ip(true);
		$para['info']='卡密充值';
		if($this->insertRow($this->prename .'member_recharge', $para)) {
			if($this->update("update {$this->prename}members set coin=coin+'{$kminfo['fee']}' where uid='{$uid}'")) {
				$this->update("update km set status=1 where km='{$amount}'");
				echo '<script>alert("充值成功！");</script>';exit;
				$data=array('code'=>1,'result'=>'充值成功！');
				echo json_encode($data);exit;
			}else {
				echo '<script>alert("充值失败！");</script>';exit;
				$data=array('code'=>0,'result'=>'充值失败！');
				echo json_encode($data);exit;
			}
		}
	}
}
?>

// if($this->user['uid']){
// $rechargeId=$this->getRechId();
// $bankid=$_REQUEST["payId"];
// $uid=$this->user['uid'];
// $amount=floatval($_REQUEST['amount']);
// $time=date('Y-m-d H:i:s', time());

// if($amount && $uid && $rechargeId){
// 	if($this->update("INSERT INTO {$this->prename}order (order_number, username, recharge_amount, state, time) VALUES('{$rechargeId}', '{$uid}', '{$amount}', '0', '{$time}')")){
// 		$para=array();
// 		$para['mBankId']=intval($bankid);
// 		$para['amount']=floatval($amount);
// 		$para['rechargeId']=$rechargeId;
// 		$para['actionTime']=$this->time;
// 		$para['uid']=$this->user['uid'];
// 		$para['username']=$this->user['username'];
// 		$para['actionIP']=$this->ip(true);
// 		if($bankid==992 || $bankid=='ZHIFUBAO'){
// 		$para['info']='支付宝扫码充值';
// 		}elseif($bankid==1004 || $bankid=='WEIXIN'){
// 		$para['info']='微信扫码充值';
// 		}else{
// 		$para['info']='用户在线充值';
// 		}
		
// 		$parter = '1658';
//    $key = '5af99338d300474f81d2add8efb40ee8';
//    $callbackurl = "http://".$_SERVER['HTTP_HOST']."/wjinc/default/userrech/Back.php";
//    $hrefbackurl = "http://".$_SERVER['HTTP_HOST']."/wjinc/default/userrech/return.php";

//    $signStr = "parter=$parter&type=$bankid&value=$amount&orderid=$rechargeId&callbackurl=$callbackurl";

//    $sign	= md5($signStr.$key);

// 		if($this->insertRow($this->prename .'member_recharge', $para)){
			
// 			   $url="http://pay.0n2.com/bank?" . $signStr . "&sign=" .$sign. "&hrefbackurl=".$hrefbackurl;
// 				header("Location: ".$url); 
						
			
// 		}else{
// 			echo '充值订单生成出错';
// 			exit;
// 		}		
// 	}else{
// 	echo '操作错误';
// 	exit;	
// 	}
// }
// }
?>